<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Business;
use App\Models\BusinessCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Validation\Rule;

class BusinessController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Business('category'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('category.name','所属分类');
            $grid->column('name');
            $grid->column('phone');
            $grid->column('image')->image(null,40,40);
//            $grid->column('longitude');
//            $grid->column('latitude');
            $grid->column('address')->limit(15);
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand(false);
                $filter->equal('id')->width(3);
                $filter->equal("cid","所属分类")->select(BusinessCategory::getOptions())->width(3);
                $filter->like('name')->width(3);
                $filter->like('phone','手机号')->width(3);
                $filter->like('address','详细地址')->width(3);
                $filter->equal("status","状态")->select(['正常','隐藏'])->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Business(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('image');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('address');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Business(), function (Form $form) {
            $form->column(6,function (Form $form){
                $form->select('cid','所属分类')->options(BusinessCategory::getOptions())->required();
                $id = $form->getKey();
                $message = ['unique' => '此合作商已存在' ];
                $rules = [
                    'required'
                ];
                if($id){
                    $rules[] = Rule::unique('business','name')->ignore($id);
                }else{
                    $rules[] = Rule::unique('business','name');
                }
                $form->text('name')->rules($rules,$message)->required();
                $form->text('phone')->required();
                $form->image('image')->autoUpload()->help("尺寸92*70")->required();
                $form->radio('status')->options(['正常','隐藏'])->default(0);
            });
            $form->column(6,function (Form $form){
                $form->hidden("latitude");
                $form->hidden("longitude");
                $form->tencent_map('latitude', 'longitude','address', '所在地址');
            });
        });
    }
}
